//
//  CenterScrollView.m
//  atlas
//
//  Created by 你好 on 17/1/16.
//  Copyright © 2017年 xyxNav. All rights reserved.
//

#import "CenterScrollView.h"

@implementation CenterScrollView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    self.isLeft=YES;
    return self;
}

-(void)configFrame
{
    self.leftButton.frame=CGRectMake(0, 0, self.bounds.size.width/2, self.bounds.size.height);
    self.rightButton.frame=CGRectMake(self.bounds.size.width/2, 0, self.bounds.size.width/2, self.bounds.size.height);
    self.lineView.frame=CGRectMake(0, self.bounds.size.height-0.5, self.bounds.size.width/2, 0.5);
}

-(void)configUI
{
    if (!self.leftButton)
    {
        self.leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
        self.leftButton.frame=CGRectMake(0, 0, self.bounds.size.width/2, self.bounds.size.height);
        [self.leftButton setImage:[UIImage imageNamed:@"type_icon"] forState:UIControlStateNormal];
        [self.leftButton setTitleColor:RGBACOLOR(1, 1, 1, 0.5) forState:UIControlStateNormal];
        [self.leftButton setTitle:@"产品展示" forState:UIControlStateNormal];
        self.leftButton.titleEdgeInsets=UIEdgeInsetsMake(0, 0, 0, -10);
        [self.leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.leftButton];
    }
    
    
    if (!self.rightButton)
    {
        self.rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
        self.rightButton.frame=CGRectMake(self.bounds.size.width/2, 0, self.bounds.size.width/2, self.bounds.size.height);
        [self.rightButton setImage:[UIImage imageNamed:@"about_icon"] forState:UIControlStateNormal];
        [self.rightButton setTitleColor:RGBACOLOR(1, 1, 1, 0.5) forState:UIControlStateNormal];
        [self.rightButton setTitle:@"关于我们" forState:UIControlStateNormal];
        self.rightButton.titleEdgeInsets=UIEdgeInsetsMake(0, 0, 0, -10);
        [self.rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.rightButton];
    }
   
    
   if(!self.lineView)
   {
       self.lineView=[[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height-0.5, self.bounds.size.width/2, 0.5)];
       self.lineView.backgroundColor=RGBACOLOR(1, 1, 1, 0.3);
       [self addSubview:self.lineView];
   }
    
   
    if (self.leftButton && self.rightButton && self.lineView)
    {
        [self layoutSubviews];
        [self setNeedsDisplay];
    }
    
    
}




-(void)leftButtonClick:(UIButton *)button
{
    [UIView animateWithDuration:0.15 animations:^{
        self.lineView.frame=CGRectMake(0, self.bounds.size.height-0.5, self.bounds.size.width/2, 0.5);
    }completion:^(BOOL finished) {
        self.isLeft=YES;
    }];
    
    if (_delegate && [_delegate respondsToSelector:@selector(didLeftButonClick:)])
    {
        [_delegate didLeftButonClick:self];
    }
}

-(void)rightButtonClick:(UIButton *)button
{
    [UIView animateWithDuration:0.15 animations:^{
        self.lineView.frame=CGRectMake(self.bounds.size.width/2, self.bounds.size.height-0.5, self.bounds.size.width/2, 0.5);
    }completion:^(BOOL finished) {
        self.isLeft=NO;
    }];
    
    if (_delegate && [_delegate respondsToSelector:@selector(didRightButtonClick:)])
    {
        [_delegate didRightButtonClick:self];
    }
}


@end
